package com.nfdw.project.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.nfdw.project.dao.MkDiPowerTradeSettleInfoMapper;
import com.nfdw.project.enums.FditemsEnum;
import com.nfdw.project.model.MkDiPowerTradeSettleInfo;
import com.nfdw.project.service.MkDiPowerTradeSettleInfoService;
import com.nfdw.project.core.AbstractService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by CodeGenerator on 2024/05/22.
 */
@Service
@Transactional
public class MkDiPowerTradeSettleInfoServiceImpl extends AbstractService<MkDiPowerTradeSettleInfo> implements MkDiPowerTradeSettleInfoService {
    @Resource
    private MkDiPowerTradeSettleInfoMapper mkDiPowerTradeSettleInfoMapper;

    /**
     * 根据条件进行筛选
     * @param mkDiPowerTradeSettleInfo
     * @return
     */
    @Override
    public MkDiPowerTradeSettleInfo findMdptsiJsqk(MkDiPowerTradeSettleInfo mkDiPowerTradeSettleInfo) {
        MkDiPowerTradeSettleInfo result = mkDiPowerTradeSettleInfoMapper.selectMdptsiJsqk(mkDiPowerTradeSettleInfo);
        if (result == null){
            result = new MkDiPowerTradeSettleInfo();
        }
        //当地区编码为空时，需要单独计算同比，电价等
        if (StringUtils.isEmpty(mkDiPowerTradeSettleInfo.getAreaCode())){
            //TODO 计算同比、电价，先置为空
            //本月结算电价
            result.setMonTradePrice(null);
            //年累计结算电价
            result.setYearTradePrice(null);
            //年累计结算电量同比
            result.setYearPowerBasisRatio(null);
            //年累计结算电价同比
            result.setYearPriceBasisRatio(null);
        }
        return result;
    }

    @Override
    public List<JSONObject>  selectMdptsiJsfx(MkDiPowerTradeSettleInfo mkDiPowerTradeSettleInfo) {
        ArrayList<JSONObject> result = new ArrayList<>();
        List<MkDiPowerTradeSettleInfo> infoList = mkDiPowerTradeSettleInfoMapper.selectMdptsiJsfx(mkDiPowerTradeSettleInfo);
        if (infoList != null && infoList.size() > 0){
            for (MkDiPowerTradeSettleInfo info : infoList) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("itemsCode",info.getItemsCode());
                jsonObject.put("itemsName", FditemsEnum.getNameByCode(info.getItemsCode()));
                info.setItemsCode(null);
                jsonObject.put("data",info);
                result.add(jsonObject);
            }
        }
        //需要单独计算电价

        return result;
    }
}
